package com.chainyoung.component_tmperson.ui.fragment;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chainyoung.common.base.BaseFragment;
import com.chainyoung.common.bean.UserInfo;
import com.chainyoung.common.bus.AppManagerEvent;
import com.chainyoung.common.bus.RxBus;
import com.chainyoung.common.constant.LibConstant;
import com.chainyoung.common.di.AppComponent;
import com.chainyoung.common.event.BaseEvent;
import com.chainyoung.common.imageloader.ImageLoader;
import com.chainyoung.common.imageloader.glide.GlideImageConfig;
import com.chainyoung.common.imageloader.glide.transformations.RoundedCornersTransformationNew;
import com.chainyoung.common.utils.CommonUtil;
import com.chainyoung.common.utils.SPUtil;
import com.chainyoung.common.utils.Utils;
import com.chainyoung.component_tmperson.R;
import com.chainyoung.component_tmperson.R2;
import com.chainyoung.component_tmperson.ui.activity.ConfirmPinCodeActivity;
import com.chainyoung.component_tmperson.ui.activity.EditUserInfoActivity;
import com.chainyoung.component_tmperson.ui.activity.HelpCenterActivity;
import com.chainyoung.component_tmperson.ui.activity.InviteFriendActivity;
import com.chainyoung.component_tmperson.ui.activity.UpdatePwdActivity;
import com.chainyoung.component_tmperson.ui.activity.cloudwallet.MyPurseAddrActivity;
import com.chainyoung.component_tmperson.ui.activity.account.RecieveAccountActivity;
import com.chainyoung.component_tmperson.ui.activity.RecommendActivity;
import com.chainyoung.component_tmperson.ui.activity.RewardActivity;
import com.chainyoung.component_tmperson.ui.activity.SetPinCodeActivity;
import com.chainyoung.component_tmperson.ui.activity.SettingActivity;
import com.chainyoung.router.RouterConstants;
import com.chainyoung.router.RouterUtils;

import org.simple.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

import static com.chainyoung.common.bus.AppManagerEvent.KILL_ALL;
import static com.chainyoung.common.constant.CommonStringConstants.URL;

@Route(path = RouterConstants.PATH_USER_CENTER)
public class UserCenterFragment extends BaseFragment {


    @BindView(R2.id.iv_logo)
    ImageView ivLogo;
    @BindView(R2.id.tv_name)
    TextView tvName;
    @BindView(R2.id.tv_user_id)
    TextView tvUserId;
    @BindView(R2.id.tv_set_pin_code)
    TextView tvSetPinCode;


    private ImageLoader imageLoader;
    private boolean isSetPinCode;


    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {

    }


    @Override
    protected void initUi() {
        super.initUi();
        setCurrentTitle(getString(R.string.tmperson_user_center));
        setUserInfos();
        setViews();
        hideKeyboard();
    }


    @Override
    protected void initData() {

    }


    @Override
    protected void onFirstUserVisible() {

    }

    @Override
    public void onUserVisible() {
        super.onUserVisible();
        setUserInfos();
        setViews();

    }

    @Override
    protected int getLayoutId() {
        return R.layout.tmperson_fragment_user_center;
    }


    private void setViews() {
        isSetPinCode = TextUtils.isEmpty(SPUtil.getString(LibConstant.PIN_CODE));
        tvSetPinCode.setText(isSetPinCode ? "去设置" : "去重置");
    }

    private void setUserInfos() {
        imageLoader = Utils.getAppComponent().imageLoader();
        UserInfo userInfo = CommonUtil.getUserInfo();

        tvName.setText(!TextUtils.isEmpty(userInfo.getUsername()) ? userInfo.getUsername() : TextUtils.isEmpty(userInfo.getMobile()) ? userInfo.getEmail() : userInfo.getMobile());
        tvUserId.setText("邀请码:" + userInfo.getInvit());
        imageLoader.loadImage(mContext,
                GlideImageConfig
                        .builder()
                        .url(userInfo.getAvatar())
                        .imagerView(ivLogo)
                        .errorPic(R.mipmap.ic_default)
                        .transformation(new RoundedCornersTransformationNew(15, 0))
                        .build());
    }


    @OnClick({R2.id.rl_1, R2.id.tv_reset_pwd, R2.id.tv_set_recieve_account, R2.id.tv_invite_friend, R2.id.tv_recommend,
            R2.id.tv_my_reward, R2.id.tv_setting, R2.id.tv_purse_addr, R2.id.tv_contact, R2.id.tv_help_center, R2.id.ll_pin_code, R2.id.tv_login_out})
    void onClick(View v) {
        if (v.getId() == R.id.rl_1) {
            launch(EditUserInfoActivity.class);
        } else if (v.getId() == R.id.tv_reset_pwd) {
            launch(UpdatePwdActivity.class);

        } else if (v.getId() == R.id.tv_set_recieve_account) {
            launch(RecieveAccountActivity.class);
        } else if (v.getId() == R.id.tv_invite_friend) {
            launch(InviteFriendActivity.class);
        } else if (v.getId() == R.id.tv_recommend) {
            launch(RecommendActivity.class);
        } else if (v.getId() == R.id.tv_my_reward) {
            launch(RewardActivity.class);
            //ToastUtil.showToast("该功能暂未开放");
        } else if (v.getId() == R.id.tv_setting) {
            launch(SettingActivity.class);
        } else if (v.getId() == R.id.tv_purse_addr) {
            launch(MyPurseAddrActivity.class);
        } else if (v.getId() == R.id.tv_contact) {
            RouterUtils.getInstance()
                    .build(RouterConstants.PATH_CHAT_ROOM)
                    .withString(URL, CommonUtil.getUserInfo().getH5_chat_url())
                    .navigation();
        } else if (v.getId() == R.id.tv_help_center) {
            launch(HelpCenterActivity.class);
        } else if (v.getId() == R.id.ll_pin_code) {
            if (isSetPinCode) {
                launch(SetPinCodeActivity.class);
            } else {
                Intent intent = new Intent(mContext, ConfirmPinCodeActivity.class);
                intent.putExtra("validateOld", true);
                intent.putExtra("pinCode", SPUtil.getString(LibConstant.PIN_CODE));
                mContext.startActivity(intent);
            }
        } else if (v.getId() == R.id.tv_login_out) {
            showLogoutDialog();
        }

    }

    private void showLogoutDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setMessage(R.string.tmperson_confirm_exit)
                .setPositiveButton(mContext.getString(R.string.confirm), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        RouterUtils.getInstance()
                                .build(RouterConstants.PATH_MAIN_LOGIN)
                                .navigation(mContext);
                        RxBus.getDefault().post(new AppManagerEvent(KILL_ALL, getString(R.string.login_out)));
                        EventBus.getDefault().post(new BaseEvent<>(BaseEvent.EventCode.LOGIN_OUT));
                        dialog.dismiss();
                    }
                })
                .setNegativeButton(mContext.getString(R.string.cancel), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });

        builder.create();
        builder.show();
    }


    private void hideKeyboard() {
        InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(ivLogo.getWindowToken(), 0); //强制隐藏键盘
    }


}
